#!/bin/bash
#********************************************************************
#Author: Andy
#Date： 2021-09-124
#FileName： ubuntu-init.sh
#URL： 
#Description： Annotated script
#********************************************************************
echo "开始执行脚本......"

#--------------------------------------------------------设置更新源--------------------------------------------------------
echo "是否更新sources.list文件, y | N?"
read sourceFlag
if [ "$sourceFlag" != "N" ];then 
echo -e "\033[41;37m开始备份并更新源.....\033[0m"
cp /etc/apt/sources.list /etc/apt/sources.list_bak
cat > /etc/apt/sources.list << EOF
# 阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

#163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

#清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

#中科大源
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF
else
	echo "不设置更新源..."
fi

#--------------------------------------------------------更新系统--------------------------------------------------------
echo "是否更新系统, y | N?"
read updateFlag
if [ "$updateFlag" != "N" ];then 
	echo -e "\033[41;37m开始更新系统.....\033[0m"
	apt-get update -y
	apt-get upgrade -y
	apt-get -y install build-essential
	apt-get autoremove openjdk*
else
	echo "不更新系统..."	
fi

#--------------------------------------------------------安装常用工具--------------------------------------------------------
echo "是否安装常用工具, y | N?"
read installFlag
if [ "$installFlag" != "N" ];then 
	echo -e "\033[41;37m开始安装常用工具.....\033[0m"
	apt-get -y purge vim-common
	apt-get -y update
	apt-get -y upgrade
	apt-get -y install vim
	apt-get install -y unzip 
	apt-get install -y wget 
	apt-get install -y git 
	apt-get install -y net-tools 
	apt-get install -y telnet 
	apt-get install -y xinetd 
	dpkg-reconfigure dash
	
	mkdir -p /software/{tools,resource}
	chown -R andy:andy /software
else
	echo "不安装软件..."	
fi

#--------------------------------------------------------设置vim--------------------------------------------------------
echo "是否设置vim, y | N?"
read vimFlag
if [ "$vimFlag" != "N" ];then 
echo -e "\033[41;37m开始是否设置.....\033[0m"
cat > /usr/share/vim/vimfiles/template.sh << EOF
#!/bin/bash
# Desc		
# Author	li-ning
# Date		


exit 0;
EOF
cat >> /etc/vim/vimrc << EOF

set nu
syntax on
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8

autocmd BufNewFile *.sh 0r /usr/share/vim/vimfiles/template.sh
EOF
else
	echo "不设置vim..."	
fi

#--------------------------------------------------------解压安装软件--------------------------------------------------------
echo "是否解压安装软件, y | N?"
read install
if [ "$install" != "N" ];then 
	tar -xzvf go1.17.2.linux-amd64.tar.gz -C /software/tools/
	tar -xzvf jdk-11.0.12_linux-x64_bin.tar.gz -C /software/tools/
	unzip apache-groovy-sdk-3.0.9.zip -d /software/tools/
	
	unzip apache-maven-3.8.3-bin.zip -d /software/tools/
	unzip gradle-7.2-bin.zip -d /software/tools/
	unzip apache-tomcat-10.0.12.zip -d /software/tools/
	unzip rocketmq-all-4.9.1-bin-release.zip -d /software/tools/
	
	tar -xzvf ideaIC-2021.2.3.tar.gz -C /software/tools/
	tar -xzvf dbeaver-ce-latest-linux.gtk.x86_64-nojdk.tar.gz -C /software/tools/
	
	unzip nacos-server-2.0.3.zip -d /software/tools/
	unzip torna-1.10.4.zip -d /software/tools/
	
	chown -R andy:andy /software
else
	echo "不安装开发环境..."	
fi

#--------------------------------------------------------安装配置开发环境--------------------------------------------------------
echo "是否配置环境变量, y | N?"
read devEnv
if [ "$devEnv" != "N" ];then 
	BASE_INSTALL=/software/tools
	echo -e "\033[41;37m开始安装JDK.....\033[0m"
	ENV_VALUE3="JAVA_HOME=$BASE_INSTALL/jdk-11.0.12"
	ENV_VALUE4='CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar'

	echo -e "\033[41;37m开始安装Groovy.....\033[0m"
	ENV_VALUE5="GROOVY_HOME=$BASE_INSTALL/groovy-3.0.9"	
	
	echo -e "\033[41;37m开始安装Go.....\033[0m"
	ENV_VALUE6="GO_HOME=$BASE_INSTALL/go"	
	
	echo -e "\033[41;37m开始安装Maven.....\033[0m"
	ENV_VALUE7="MAVEN_HOME=$BASE_INSTALL/apache-maven-3.8.3"	
	
	echo -e "\033[41;37m开始安装Gradle.....\033[0m"
	ENV_VALUE8="GRADLE_HOME=$BASE_INSTALL/gradle-7.2"	
	ENV_VALUE9="GRADLE_USER_HOME=/software/resource/gradle-repository"	
	
	ENV_VALUE10="TOMCAT_HOME=$BASE_INSTALL/apache-tomcat-9.0.55"	
	ENV_VALUE11="CATALINA_HOME=$TOMCAT_HOME"	
	
	ENV_VALUE12='PATH=$PATH:$JAVA_HOME/bin:$GROOVY_HOME/bin:$GO_HOME/bin:$MAVEN_HOME/bin:$GRADLE_HOME/bin:$TOMCAT_HOME/bin:'
cat >> /etc/profile << EOF

# ----------------------------------------设置开发环境变量----------------------------------------
# JDK
$ENV_VALUE3
$ENV_VALUE4

# GROOVY
$ENV_VALUE5

# GO
$ENV_VALUE6

# MAVEN
$ENV_VALUE7

# GRADLE
$ENV_VALUE8
$ENV_VALUE9

# NACOS
$ENV_VALUE10

# TORNA
$ENV_VALUE11

# PATH
$ENV_VALUE12

# auto start
/software/auto-start.sh

EOF

source /etc/profile
else
	echo "不安装开发环境..."	
fi

cat > /software/auto-start.sh << EOF
#!/bin/bash
#
# Desc
# Authorli-ning
# Date

# tornal start
cd /software/tools/torna-1.9.0/
./torna-startup.sh

# nacos start
cd /software/tools/nacos/bin
nacos-startup.sh -m standalone

exit 0;
EOF

#--------------------------------------------------------安装redis6--------------------------------------------------------
echo "是否安装redis, y | N?"
read redisFlag
if [ "$redisFlag" != "N" ];then 
	echo -e "\033[41;37m开始安装redis.....\033[0m"
	apt-get install -y redis-server
	systemctl enable redis-server.service
	
	echo -e "\033[41;37m请设置bind、requirepass并重启服务service redis-server status | start | stop | restart\033[0m"
else
	echo "不安装redis..."	
fi

#--------------------------------------------------------安装mysql8--------------------------------------------------------
echo "是否安装mysql, y | N?"
read mysqlFlag
if [ "$mysqlFlag" != "N" ];then 
	echo -e "\033[41;37m开始安装mysql.....\033[0m"
	sudo apt-get remove -y mysql-client-5.7
	sudo apt-get remove -y mysql-client-core-5.7
	sudo apt-get remove -y mysql-common 
	sudo apt-get remove -y mysql-server  
	sudo apt-get remove -y mysql-server-5.7
	sudo apt-get remove -y mysql-server-core-5.7 

	dpkg -i mysql-apt-config_0.8.19-1_all.deb
	apt-get -y update
	apt-get -y install mysql-server
	echo -e "\033[41;37mmysql安装完成，请使用命令：service mysql start | status | stop 操作.....\033[0m"
else
	echo "不安装mysql..."	
fi

#--------------------------------------------------------安装mongodb--------------------------------------------------------
echo "是否安装mongodb, y | N?"
read mongoFlag
if [ "$mongoFlag" != "N" ];then 
	echo -e "\033[41;37m开始安装mongoDB.....\033[0m"
	apt-get -y install mongodb
	pgrep mongo -l
	echo -e "\033[41;37mmongoDB安装完成，请使用命令：service mongodb start | status | stop 操作.....\033[0m"
	
else
	echo "不安装mongoDB..."	
fi

echo -e "\033[41;37m系统设置完成.....\033[0m"
echo -e "本机信息如下.........."
echo -e "主机名："`hostname`
echo -e "IP地址："`ifconfig | grep broadcast | awk "NR==1" | awk -F" " '{print $2}'`
exit 0
